package com.miguo.bean;

import com.miguo.app.AppContext;
import com.miguo.framework.request.JsonData;

public class UserBean {

    private String token;

    private Long token_expires_time;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getTokenExpiresTime() {
        return token_expires_time;
    }

    public void setTokenExpiresTime(Long token_expires_time) {
        this.token_expires_time = token_expires_time;
    }

    public UserBean() {
    }

    public void setUserToken(AuthLoginBean user_token) {
        token = user_token.getToken();
        token_expires_time = user_token.getTokenExpiresTime();
        isLogin = true;
        AppContext.Prefs().setUserToken(token);
        AppContext.Prefs().setUserTokenExpiresTime(token_expires_time);
    }

    /**
     * 获取用户详细信息
     */
    public void setUserInfo(JsonData info) {
        if (info.has("token")) {
            token = info.optString("token");
        }

        if (info.has("token_expires_time")) {
            token_expires_time = info.optLong("token_expires_time");
        }
        if (token != null && token_expires_time != null) {
            isLogin = true;
            AppContext.Prefs().setUserToken(token);
            AppContext.Prefs().setUserTokenExpiresTime(token_expires_time);
        }

        // 用户名
        if (info.has("username")) {
            username = info.optString("username");
        }
        // 积分
        if (info.has("score")) {
            score = info.optString("score");
        }
        // 收货姓名
        if (info.has("truename")) {
            truename = info.optString("truename");
        }
        // 收货人电话
        if (info.has("telephone")) {
            telephone = info.optString("telephone");
        }
        // 省份
        if (info.has("province")) {
            province = info.optString("province");
        }
        // 城市
        if (info.has("city")) {
            city = info.optString("city");
        }
        // 区域
        if (info.has("town")) {
            town = info.optString("town");
        }

        // 街道详细地址
        if (info.has("address")) {
            address = info.optString("address");
        }

        // 邮编
        if (info.has("postcode")) {
            postcode = info.optString("postcode");
        }

        area = String.format("%s %s %s", province, city, town);
    }

    // 用户名
    private String username = "";

    // 积分
    private String score = "";

    // 收货姓名
    private String truename = "";

    // 收货人电话
    private String telephone = "";

    // 省份
    private String province = "";

    // 城市
    private String city = "";

    // 区域
    private String town = "";

    // 街道详细地址
    private String address = "";

    // 省市区
    private String area = "";

    // 邮编
    private String postcode = "";

    // 登录状态
    private boolean isLogin = false;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getTruename() {
        return truename;
    }

    public void setTruename(String truename) {
        this.truename = truename;
    }

    public String getTelePhone() {
        return telephone;
    }

    public void setTelePhone(String telephone) {
        this.telephone = telephone;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    public boolean getIsLogin() {
        return isLogin;
    }

    public void setIsLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }

    public void Logout() {
        AppContext.Prefs().setUserToken(null);
        AppContext.Prefs().setUserTokenExpiresTime(0);
        this.isLogin = false;
    }

}
